//
//  BackBoxCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/29.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "BackBoxCell.h"

@interface BackBoxCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *NOLab;
@property (nonatomic, strong) UILabel *ownTitleLab;
@property (nonatomic, strong) UILabel *ownLab;
@property (nonatomic, strong) UILabel *rentTitleLab;
@property (nonatomic, strong) UILabel *rentLab;
@property (nonatomic, strong) UILabel *statusLab;

@property (nonatomic, strong) UIButton *chooseBtn;

@end

@implementation BackBoxCell

+ (instancetype)BackBoxCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"BackBoxCell";
    BackBoxCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[BackBoxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.chooseBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, (100 - 23) * 0.5, 23, 23)];
        [self.chooseBtn addTarget:self action:@selector(chooseClick) forControlEvents:UIControlEventTouchUpInside];
        self.chooseBtn.hidden = YES;
        [self.contentView addSubview:self.chooseBtn];
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, ScreenW - 30, 100)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.NOLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, self.bgView.nim_width - 76 - 10 - 10, 19)];
        self.NOLab.font = BoldFontOfSize(18);
        self.NOLab.textColor = Color_ThemeColor;
        [self.bgView addSubview:self.NOLab];
        
        self.statusLab = [[UILabel alloc] initWithFrame:CGRectMake(self.bgView.nim_width - 76 + 1, 0, 76, 30)];
        self.statusLab.textAlignment = NSTextAlignmentCenter;
        self.statusLab.font = BoldFontOfSize(12);
        [self.bgView addSubview:self.statusLab];
        [self.statusLab setBorderWithCornerRadius:6 borderWidth:0 borderColor:[UIColor clearColor] type:UIRectCornerTopRight];
        [self.statusLab setBorderWithCornerRadius:12 borderWidth:0 borderColor:[UIColor clearColor] type:UIRectCornerBottomLeft];
        
        self.ownTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 10 + self.NOLab.nim_bottom, [FactoryMethod sizeWithText:@"所有方：" font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(0, 0)].width, 15)];
        self.ownTitleLab.text = @"所有方：";
        self.ownTitleLab.textColor = HexColor(0x9C9DB3);
        self.ownTitleLab.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.ownTitleLab];
        
        self.ownLab = [[UILabel alloc] initWithFrame:CGRectMake(self.ownTitleLab.nim_right, self.ownTitleLab.nim_top, self.bgView.nim_width - 10 - self.ownTitleLab.nim_right, self.ownTitleLab.nim_height)];
        self.ownLab.textColor = HexColor(0x2B2C44);
        self.ownLab.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.ownLab];
        
        self.rentTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 10 + self.ownTitleLab.nim_bottom, [FactoryMethod sizeWithText:@"租用方：" font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(0, 0)].width, 15)];
        self.rentTitleLab.text = @"租用方：";
        self.rentTitleLab.textColor = HexColor(0x9C9DB3);
        self.rentTitleLab.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.rentTitleLab];
        
        self.rentLab = [[UILabel alloc] initWithFrame:CGRectMake(self.rentTitleLab.nim_right, self.rentTitleLab.nim_top, self.bgView.nim_width - 10 - self.rentTitleLab.nim_right, self.rentTitleLab.nim_height)];
        self.rentLab.textColor = HexColor(0x2B2C44);
        self.rentLab.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.rentLab];
        
        self.height = self.bgView.nim_bottom + 10;
    }
    return self;
}

-(void)setModel:(TurnOverBoxModel *)model {
    _model = model;
    if (model.canChoose) {
        self.chooseBtn.hidden = NO;
        self.chooseBtn.frame = CGRectMake(15, (100 - 23) * 0.5, 23, 23);
        self.bgView.frame = CGRectMake(self.chooseBtn.nim_right + 15, 0, ScreenW - 30, 100);
        if (model.isSelected) {
            [self.chooseBtn setImage:[UIImage imageNamed:@"icon_backBox_choice_sel"] forState:0];
        }else {
            [self.chooseBtn setImage:[UIImage imageNamed:@"icon_backBox_choice"] forState:0];
        }
    }else {
        self.chooseBtn.hidden = YES;
        self.chooseBtn.frame = CGRectZero;
        self.bgView.frame = CGRectMake(15, 0, ScreenW - 30, 100);
    }
    self.NOLab.text = model.boxNO;
    self.ownLab.text = model.ownMaster;
    self.rentLab.text = model.rentMaster;
    if (model.status == TurnOverBoxStatusUsing) {
        self.statusLab.text = @"使用中";
        self.statusLab.backgroundColor = HexColorA(0xFBA962, 0.1);
        self.statusLab.textColor = HexColor(0xFBA962);
    }else if (model.status == TurnOverBoxStatusEmpty) {
        self.statusLab.text = @"空置";
        self.statusLab.backgroundColor = HexColorA(0x3D68EB, 0.1);
        self.statusLab.textColor = HexColor(0x3D68EB);
    }else if (model.status == TurnOverBoxStatusWaitBack) {
        self.statusLab.text = @"待回收";
        self.statusLab.backgroundColor = HexColorA(0x9C9DB3, 0.1);
        self.statusLab.textColor = HexColor(0x9C9DB3);
    }
}

- (void)chooseClick {
    if (self.model.canChoose) {
        if (self.chooseBlock) {
            self.chooseBlock();
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
